package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜品分类管理")
@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增菜品分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation("新增分类")
    @PostMapping
    @Cacheable(cacheNames = "setmealCache",key = "setmealDTO.categoryId")
    public Result newCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增菜品分类:{}",categoryDTO);
        categoryService.newCategory(categoryDTO);
        return Result.success();
    }


    /**
     * 启用禁用菜品
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("禁用菜品")
    @PostMapping("/status/{status}")
    public Result status(Long id , @PathVariable Integer status){
        log.info("禁用菜品:{}",id);
        categoryService.status(id,status);
        return Result.success();
    }


    /**
     * 删除菜品
     * @param id
     * @return
     */
    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result delete(Long id){
        log.info("删除菜品:{}",id);
        categoryService.delete(id);
        return Result.success();
    }



    @ApiOperation("根据类型查询")
    @GetMapping("/list")
    public Result<List<Category>> categoryInfo(Integer type){
        log.info("根据类型查询");
        List<Category> list = categoryService.categoryInfo(type);
        return Result.success(list);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改菜品分类:{}",categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO pageQueryDTO){
        log.info("分类分页查询");
        PageResult pageResult = categoryService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

}
